//
//  RwdStatusViewModel.swift
//  Weibo_rwd
//
//  Created by 任文迪 on 16/7/14.
//  Copyright © 2016年 spaceX. All rights reserved.
//

import UIKit

class RwdStatusViewModel: NSObject {
    
    
    
    //  转发数显示内容
    var retweetStr: String?
    //  评论数显示内容
    var commentStr: String?
    //  赞数显示内容
    var unlikeStr: String?
    //  转发微博的内容
    var retweetContens:String?
    //会员等级图片
    var mbRankImage:UIImage?
    //会员分类图片
    var verified_typeImage:UIImage?
    //微博来源分类
    var sourceContent:String?
    var status:RwdStatusModel?
    init(status:RwdStatusModel) {
        super.init()
        self.status = status
        
        retweetStr = handleCount(status.reposts_count, defaultTitle: "转发")
        commentStr = handleCount(status.comments_count, defaultTitle: "评论")
        unlikeStr = handleCount(status.attitudes_count, defaultTitle: "赞")
        
        handleRetweet(status)
        //处理会员等级
        handlembRankImage(status.user?.mbrank ?? 0)
        //处理会员分类
        handleVerified_type(status.user?.verified_type ?? 0)
        //处理微博来源
        handleSource(status.source ?? "")
    }
    //处理微博来源分类
    private func handleSource(source:String) {
        guard let range1 = source.rangeOfString("\">"),let range2 = source.rangeOfString("</") else {
            return
        }
        let startIndex = range1.endIndex
        let endIndex = range2.startIndex
        
        sourceContent = "来自 " + source.substringWithRange(startIndex..<endIndex)
    }
    
    
    //处理会员分类
        //  认证类型等级 认证类型 -1 没有认证 ，0 认证用户，2，3，5 企业认证 ， 220 达人
    private func handleVerified_type(verified_type:Int) {

        switch (verified_type){
        case 0:
            verified_typeImage = UIImage(named: "avatar_vip")
        case 2,3,5:
            verified_typeImage = UIImage(named: "avatar_enterprise_vip")
        case 220:
            verified_typeImage = UIImage(named: "avatar_grassroot")
        default:
            verified_typeImage = nil
            
        }
        
        
        
    }
    //处理会员等级显示的图片
    private func handlembRankImage(mbrank:Int) {
    if mbrank>=1 && mbrank<=6{
        let image = UIImage(named: "common_icon_membership_level\(mbrank)")
        mbRankImage = image
    
    }
    
    
    }
    //c处理转发微博模型数据
    private func handleRetweet(status:RwdStatusModel) {
        if status.retweeted_status != nil {
            guard let name = status.retweeted_status?.user?.screen_name,let content = status.retweeted_status?.text else {
                
                return
            }
            
            retweetContens = "@\(name):\(content)"
            
        }
        
    }
    
    //设置底部toolbar的显示
    private func handleCount(count: Int, defaultTitle: String) -> String {
        
        if count > 0 {
            if count >= 10000 {
                //  进行格式化处理
                let result = Float(count / 1000) / 10
                
                var resultStr = "\(result)万"
                
                if resultStr.containsString(".0") {
                    resultStr = resultStr.stringByReplacingOccurrencesOfString(".0", withString: "")
                }
                
                return resultStr
                
                
                
            } else {
                return "\(count)"
            }
            
        } else {
            return defaultTitle
        }
        
        
        
        
    }
    
    
}
